package cn.demomaster.quickpermission.sample;


import static cn.demomaster.quickpermission.QuickPermission2.TAG;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

import cn.demomaster.quickpermission.QuickPermission;
import cn.demomaster.quickpermission.QuickPermission2;
import cn.demomaster.quickpermission.Requester;
import cn.demomaster.quickpermission.Requester2;
import cn.demomaster.quickpermission.listener.PermissionResultCallback;
import cn.demomaster.quickpermission.model.PermissionModel;
import cn.demomaster.quickpermission.util.PermissionGroupUtil;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.functions.Function3;

public class MainActivity extends AppCompatActivity {

    String[] permissions = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.SYSTEM_ALERT_WINDOW,};
//    Manifest.permission.READ_EXTERNAL_STORAGE,
//    Manifest.permission.WRITE_EXTERNAL_STORAGE,
//    Manifest.permission.INSTALL_PACKAGES,

    LinearLayout ll_root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn_all = findViewById(R.id.btn_all);
        btn_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //QuickPermission.with(MainActivity.this).permissions(new String[]{""});
                //QuickPermission.with(MainActivity.this).permissions(new ArrayList<String>());
                QuickPermission.with(MainActivity.this)
                        .permissions(permissions)
                        .onStart(new Function1<Requester, Unit>() {
                            @Override
                            public Unit invoke(Requester requester) {
                                new AlertDialog.Builder(MainActivity.this)
                                        .setTitle("权限请求")
                                        .setMessage("为了能够正常使用本应用的所有功能，我们需要以下权限：\n\n" +
                                                "1. 读取外部存储 - 用于访问您的文件。\n" +
                                                "2. 写入外部存储 - 用于保存文件。\n" +
                                                "3. 摄像头 - 用于拍照或录像。\n" +
                                                "4. 读取联系人 - 用于访问您的联系人信息。\n" +
                                                "5. 显示系统窗口 - 用于显示悬浮窗。\n" +
                                                "6. 安装包 - 用于安装应用。\n\n" +
                                                "请允许这些权限以确保应用正常运行。")
                                        .setPositiveButton("确定", (dialog, which) -> requester.continueRequest(MainActivity.this))
                                        .setNegativeButton("取消", (dialog, which) -> {requester.cancelRequest(MainActivity.this);
                                        })  // 如果用户拒绝，关闭应用
                                        .show();
                                return null;
                            }
                        })
                        .onStart2(new Function3<Activity, String, Requester2, Unit>() {
                            @Override
                            public Unit invoke(Activity activity, String s, Requester2 requester2) {
                                new AlertDialog.Builder(MainActivity.this)
                                        .setTitle("权限请求")
                                        .setMessage("为了能够正常使用本应用的所有功能，我们需要以下权限：\n\n" +
                                                s + "\n\n" +
                                                "请允许这些权限以确保应用正常运行。")
                                        .setPositiveButton("确定", (dialog, which) -> requester2.continueRequest2(activity, s))
                                        .setNegativeButton("取消", (dialog, which) -> {requester2.cancelRequest2(activity);
                                        })  // 如果用户拒绝，关闭应用
                                        .show();
                                return null;
                            }
                        })
                        .onRationale(new Function1<String, Unit>() {
                            @Override
                            public Unit invoke(String s) {
                                //System.out.println("权限被拒绝，请前往应用设置授权操作。"+deniedPermissions[0])
                                //Toast.makeText(MainActivity.this, "权限被拒绝，请前往应用设置授权操作。", Toast.LENGTH_LONG).show();
                                new AlertDialog.Builder(MainActivity.this)
                                        .setTitle("授权失败")
                                        .setMessage("权限多次被拒绝，请前往应用详情页授权操作。")
                                        .setPositiveButton("ok", (dialog, which) -> {})  // 如果用户拒绝，关闭应用
                                        .show();
                                return null;
                            }
                        })
                        .onGranted(new Function0<Unit>() {
                            @Override
                            public Unit invoke() {
                                Toast.makeText(MainActivity.this, "all通过", Toast.LENGTH_LONG).show();
                                return null;
                            }
                        })
                        .onDenied(new Function1<String, Unit>() {
                            @Override
                            public Unit invoke(String s) {
                                new AlertDialog.Builder(MainActivity.this)
                                        .setTitle("授权失败" + s)
                                        .setMessage("为了能够正常使用，我们需要您的"+s + "权限。请允许我们访问此权限，或前往应用设置页授权。")
                                        .setPositiveButton(
                                                "好的"
                                                , (dialog, which) -> {
                                                })
                                        .show();
                                return null;
                            }
                        })
                        .request();
//                QuickPermission2.with()
//                        .permissions(permissions)
//                        .callback(new PermissionResultCallback() {
//
//                            @Override
//                            public void onPermissionsGranted(String[] permissions) {
//                                Toast.makeText(MainActivity.this, "all通过", Toast.LENGTH_LONG).show();
//                            }
//
//                            @Override
//                            public void onPermissionsDenied() {
//                                Toast.makeText(MainActivity.this, "all拒绝", Toast.LENGTH_LONG).show();
//                            }
//                        })
//                        .request();
//                QuickPermission.requestPermission(MainActivity.this,permissions, new QuickPermission.PermissionListener() {
//                    @Override
//                    public void onPassed() {
//                        Toast.makeText(MainActivity.this, "all通过", Toast.LENGTH_LONG).show();
//                    }
//
//                    @Override
//                    public void onRefused() {
//                        Toast.makeText(MainActivity.this, "all拒绝", Toast.LENGTH_LONG).show();
//                    }
//                });
            }
        });

        ll_root = findViewById(R.id.ll_root);
        for (String permission : permissions) {
            PermissionModel model = PermissionGroupUtil.getPermissionModel(permission);
            Button button = new Button(MainActivity.this);
            button.setTag(model);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PermissionModel model1 = (PermissionModel) v.getTag();
                    QuickPermission.with(MainActivity.this)
                            .permissions(new String[]{model1.getName()})
                            .onGranted(new Function0<Unit>() {
                                @Override
                                public Unit invoke() {
                                    Toast.makeText(MainActivity.this, "通过", Toast.LENGTH_LONG).show();return null;
                                }
                            })
                            .request();

//                    QuickPermission.with(MainActivity.this).permissions(
//                                    new String[]{
//                                            Manifest.permission.READ_EXTERNAL_STORAGE,
//                                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                                            Manifest.permission.WRITE_SETTINGS,
//                                            Manifest.permission.SYSTEM_ALERT_WINDOW})
//                            .onGranted(new Function0<Unit>() {
//                                @Override
//                                public Unit invoke() {
//                                    Toast.makeText(MainActivity.this, "all通过", Toast.LENGTH_LONG).show();
//                                    return null;
//                                }
//                            }).request();
//                    QuickPermission2.with()
//                            .permissions(new String[]{
//                                    model1.getName()})
//                            .callback(new PermissionResultCallback() {
//                                @Override
//                                public void onPermissionsGranted(String[] permissions) {
//                                    Toast.makeText(MainActivity.this, "通过" + String.join(", ", permissions), Toast.LENGTH_LONG).show();
//                                }
//
//                                @Override
//                                public void onPermissionsDenied() {
//                                    Toast.makeText(MainActivity.this, "拒绝" + String.join(", ", permissions), Toast.LENGTH_LONG).show();
//                                }
//                            })
//                            .request();
//                    QuickPermission.requestPermission(MainActivity.this, new String[]{
//                            model1.getName()}, new QuickPermission.PermissionListener() {
//                        @Override
//                        public void onPassed() {
//                            Toast.makeText(MainActivity.this, "通过", Toast.LENGTH_LONG).show();
//                        }
//
//                        @Override
//                        public void onRefused() {
//                            Toast.makeText(MainActivity.this, "拒绝", Toast.LENGTH_LONG).show();
//                        }
//                    });
                }
            });
            Log.i("T", "" + permission);
            button.setText(model.getDesc());
            ll_root.addView(button);
        }

        // QuickPermission1.Companion.getInstance()
//        QuickPermission.get()
//                .setCallback(null)
//                .request();

        QuickPermission.requestClipBoardPermission(this.ll_root.getContext());
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //Log.e(TAG, "onRequestPermissionsResult: "+permissions);
        //QuickPermission2.getInstance().onRequestPermissionsResult(MainActivity.this,requestCode,permissions,grantResults);
        QuickPermission.onRequestPermissionsResult(MainActivity.this, requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //Log.e(TAG, "onActivityResult: "+permissions);
        //QuickPermission2.getInstance().onActivityResult(MainActivity.this,requestCode, resultCode, data);
        QuickPermission.onActivityResult(MainActivity.this, requestCode, resultCode, data);
    }
}